package com.uchengwang.ui.cm_ui.me.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.cycle.adapter.BaseCycleAdapter;
import com.uchengwang.ui.cm_ui.cycle.viewHolder.BaseViewHolder;
import com.uchengwang.ui.cm_ui.login.bean.UserBean;

import java.util.List;


/**
 * Created by U城网 on 2017/7/11.
 */

public class MembersAdapter extends BaseCycleAdapter<UserBean, RecyclerView.ViewHolder>{
    private Context context;
    private int type = 0;//0普通成员查看  6推荐给好友 5邀请好友加群 4选择好友建群
    private long owenId;//群主ID
    private List<String> members;

    public MembersAdapter(Context context, int type){
        this.context = context;
        this.type = type;
    }

    public void setOwnId(long owenId){
        this.owenId = owenId;
    }

    /**
     * 群成员 当推荐给好友时用到
     * */
    public void setMembers(List<String> members){
        this.members = members;
    }

    /**
     * 显示移除
     * */
    public void setType(int type){
        this.type = type;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_layout_view, parent, false);
        return new BaseViewHolder(view, type);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, final int position) {
        final BaseViewHolder holder = (BaseViewHolder) h;
        final UserBean userBean = list.get(position);
        if (!TextUtils.isEmpty(userBean.getUserAvatar())){
            ImageManager.getInstance().loadImage(context, holder.head, userBean.getUserAvatar(), ImageManager.ImageType.CIRCLE);
        }else {
            ImageManager.getInstance().loadImage(context, holder.head, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
        }
        String remarkName = userBean.getRemarkName();
        if (!TextUtils.isEmpty(remarkName)){
            holder.name.setText(remarkName);
        }else {
            holder.name.setText(userBean.getNickName());
        }

        if (userBean.getUserRole() == BaseConfig.STUDENT_ROLE){
            holder.apply_sex.setVisibility(View.VISIBLE);
            if (userBean.getSex() ==0){
                ImageManager.getInstance().loadImage(context, holder.apply_sex, R.drawable.icon_woman, ImageManager.ImageType.NARMOL);
            }else {
                ImageManager.getInstance().loadImage(context, holder.apply_sex, R.drawable.icon_man, ImageManager.ImageType.NARMOL);
            }
        }else {
            holder.apply_sex.setVisibility(View.GONE);
        }

        String desc = userBean.getUserSign();
        holder.des.setText(TextUtils.isEmpty(desc) ? "暂无个性标语" : desc);

        //在社团成员列表里
        if (owenId >0){
            if (userBean.getId() == owenId){
                //当前用户和社长账号相同
                holder.apply_type.setVisibility(View.VISIBLE);
                holder.apply_type.setText("群主");
            }else {
                holder.apply_type.setVisibility(View.GONE);
            }
        }

        switch (type){
            case 4://选择好友建群
                holder.apply_checkBox.setVisibility(View.VISIBLE);
                break;
            case 6://推荐给好友
            case 5://5邀请好友加群
                if (isMember(String.valueOf(userBean.getId()))){
                    //是群成员
                    holder.apply_checkBox.setVisibility(View.INVISIBLE);
                }else {
                    holder.apply_checkBox.setVisibility(View.VISIBLE);
                }
                break;
        }

        holder.apply_checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                userBean.setCheck(b);
            }
        });

        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (type){
                    case 0:
//                        if (userBean.getUserRole() ==BaseConfig.CLUB_ROLE){
//                            Intent intent = new Intent(context, ClubDetailActivity.class);
//                            intent.putExtra(BaseConfig.CLUB_ID, userBean.getId());
//                            context.startActivity(intent);
//                        }else {
//                            UCommonUtil.getUtil().toUserActivity((Activity) context, userBean.getId());
//                        }

                        break;
                    case 6://推荐给好友
                    case 4://选择好友建群
                    case 5://邀请好友加群
                        userBean.setCheck(!userBean.isCheck());
                        holder.apply_checkBox.setChecked(userBean.isCheck());
                        break;
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    /**
     *判断此人是否已是成员
     */
    private boolean isMember(String uid){
        if (members != null && members.size() >0){
            int count = members.size();
            for (int i=0; i < count; i++){
                if (uid.equals(members.get(i))){
                    return true;
                }
            }
            return false;
        }else {
            return false;
        }
    }


}
